﻿using JinJun.YShow.BLL.IService;
using JinJun.YShow.Model;
using JinJun.YShow.Web.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace JinJun.YShow.Web.Controllers
{
    public class BrandController :BaseController
    {      

        private IBrandService _brandService;       

        public BrandController(IBrandService brandService)
        {
            _brandService = brandService;
        }


        /// <summary>
        /// 品牌分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult QueryPageList(int page, int limit,string username)
        {
            //创建分页类对象
            PageListResult<Brand> pageListResult = new PageListResult<Brand>();
            //赋值
            pageListResult.code = 0;
            pageListResult.msg = string.Empty;
            int count = 0;
            //创建以Brand为类型的List对象
            List<Brand> brands = new List<Brand>();
            //表达式
            Expression<Func<Brand, bool>> whereLambda = a => true;
            if (!string.IsNullOrEmpty(username))
            {
                whereLambda = a => a.Name.Contains(username);
            }
            Expression<Func<Brand, int>> orderbyLambda = a => a.Id;
            //调用分页查询
            pageListResult.data = _brandService.QueryPageList(page, limit, whereLambda, orderbyLambda, out count);
            pageListResult.count = count;
            return Json(pageListResult, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 批量删除根据Id
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult BatchDeleteById(string id)
        {
            //用string类型数组去接收所传参数拆分后的值
            string[] num1 = id.Split(',');
            //将num1中的值用int类型数组去接收
            int[] num2 = new int[num1.Length];
            for (int i = 0; i < num2.Length; i++)
            {
                num2[i] = Convert.ToInt32(num1[i]);
            }
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();
            //调用批量删除方法
            operateResult.Success = _brandService.BatchDeleteById(num2);
            return Json(operateResult, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 删除功能
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult Delete(Brand brand)
        {
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();
            //调用删除方法
            operateResult.Success = _brandService.Delete(brand);
            return Json(operateResult, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 修改下架状态
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult UpdateStatus(Brand brand)
        {
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();
            brand.UpdateTime = DateTime.Now;
            //调用修改方法
            operateResult.Success = _brandService.Update(brand);
            return Json(operateResult, JsonRequestBehavior.AllowGet);
        }


        /// <summary
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UploadImage(HttpPostedFileBase file)
        {
            //先定义文件
            string fileName = Guid.NewGuid().ToString("N");
            //图片存放的路径
            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Resource/Uploads");
            //如果没有图片
            if (Request.Files.Count == 0)
            {
                return Json(new { Success = false, FilePath = "" });
            }
            //获取图片的扩展名
            string stuf = Path.GetExtension(file.FileName);

            //图片存放路径不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            //保存图片
            string path = Path.Combine(filePath, fileName + stuf);
            file.SaveAs(path);

            //返回前端
            return Json(new { Success = true, FilePath = "/Resource/Uploads/" + fileName + stuf });
        }



        /// <summary>
        /// 添加品牌
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult AddBrandInfo(Brand brand)
        {
            //创建操作类对象 
            OperateResult operateResult = new OperateResult();
            brand.UpdateTime = DateTime.Now;
            //调用添加方法
            operateResult.Success = _brandService.Add(brand);
            return Json(operateResult, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 编辑品牌信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult EditArticleById(Brand brand)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            brand.UpdateTime = DateTime.Now;
            //调用修改方法
            operateResult.Success = _brandService.Update(brand);
            return Json(operateResult, JsonRequestBehavior.AllowGet);
        }



    }
}